/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.java.tools;

import com.jb.framework.util.BeanUtil;
import com.jb.util.ClassUtil;
import com.jb.web.bean.event.UIEventHandler;


public class DynamicEventDispatcher extends InterfaceRunner {
    private String mCode;

    public DynamicEventDispatcher(String pPackageName, String pClassName, String pCode) {
        super(pPackageName, pClassName, com.jb.web.bean.event.UIEventHandler.class);
        mCode = pCode;
    }

    public String getCode() {
        if (mCode != null) {
            return mCode;
        }

        StringBuffer code = new StringBuffer();

        if ((getPackageName() != null) && (getPackageName().length() != 0)) {
            code.append("package " + getPackageName() + ";\r\n");
        }

        code.append("\r\n/** \r\n")
            .append("This code is generated as a template.\r\n You need to implement your event logic in method handleEvent \r\n")
            .append("**/\r\n\r\n").append("import java.util.*;\r\n").append("import com.jb.web.bean.*;\r\n")
            .append("import com.jb.web.bean.event.*;\r\n").append("import com.jb.web.context.*;\r\n")
            .append("\n\rpublic class " + getClassName() + " implements " + getInterface().getName() + "\r\n")
            .append("  {").append("\r\n  public void handleEvent(WebContext pCtx, UIEvent event) \r\n{")
            .append("\r\n  //  Gets parameter;\r\n")
            .append("\r\n  //  String testParam = (String) event.getParameter(\"testParam\");\r\n")
            .append("\r\n  //  Gets component that event is raisen;\r\n")
            .append("\r\n  //  HtmlItem sourceObject = (HtmlItem) event.getSource();\r\n")
            .append("\r\n  //  Sets value for the source object;\r\n")
            .append("\r\n  //  sourceObject.setValue(\"Hello World\");\r\n")
            .append("\r\n    System.out.println(\"" + System.currentTimeMillis() + " \");\r\n").append("\r\n    ")
            .append("\r\n  }\r\n}\r\n");

        mCode = code.toString();

        return code.toString();
    }

    //interface of runner
    public void execute() {
       
        UIEventHandler eventHandler = (UIEventHandler) ClassUtil.createInstance(getClassQName());
        if(eventHandler!=null)
            eventHandler.handleEvent(null, null);
    
    }
}
